<template>
	<view style="overflow: scroll;">
		<view class="headcontent">
			<view class="contentbox" :style="'height:'+headHeight+'rpx;'">
				<image src="../../static/images/cc/left.png" mode="widthFix" @click="goBack"></image>
			</view>
		</view>
		<view class="banner">
			<swiper @change="change_banner" :indicator-dots="false" :autoplay="true" :circular="true" :interval="3000" :duration="1000"
				indicator-active-color="#84BD00" indicator-color="rgba(66, 137, 243, 0.2)">
				<swiper-item v-for="(item,index) in banner" :key="index">
					<view class="swiper-item">
						<image :src="$wanlshop.oss(item, 0, 0)" mode="widthFix"></image>
					</view>
				</swiper-item>
				<!-- <swiper-item>
					<view class="swiper-item">
						<image
							src="https://qcloud.dpfile.com/pc/UR3o4Rr6AE7MnIxWiwyTExNGm0jeRZnoxf9PPj20DO0opzl7UKxf251v2HlD_Lu0.jpg"
							mode="widthFix"></image>
					</view>
				</swiper-item> -->
			</swiper>
			<view class="shadow"></view>
			<view class="dropBox">
				<!-- <view class="drop select"></view> -->
				<view class="drop" :key="index" v-for="(item,index) in banner" :class="index==current_banner_index?'select':''"></view>
			</view>
		</view>
		<view class="shop">
			<view class="shopAbox">
				<view class="shopCon">
					<!-- <image src="../../../static/images/cc/shoplogo.png" mode="widthFix"></image> -->
					<image v-if="shopData.avatar" :src="$wanlshop.oss(shopData.avatar, 0,0)" mode="aspectFill"></image>
					<view class="shopDetail">
						<view class="shopName">{{shopData.shopname}}</view>
						<view class="shopNum"><text>{{shopData.find_user.fans}}</text> 关注</view>
					</view>
				</view>
				<view class="follow" v-if="shopData.isFollow === 0" @click="handleFollow">+关注</view>
				<view class="follow" v-else @click="handleFollow">取关</view>
			</view>
			<view class="shopBbox">
				<view class="shopLcon">
					<view class="divide">{{shopData.score_service}}</view>
					<view class="star">
						<image src="../../static/images/cc/star1.png" mode="widthFix" v-for="(item,index) in shopData.score_service" :key="index"></image>
						<image src="../../static/images/cc/star2.png" mode="widthFix" v-for="(item,index) in 5-shopData.score_service" :key="index"></image>
					</view>
				</view>
				<view class="label">
					<view>月销{{shopData.monthly_sales_order}}+</view>
					<view>人均￥{{shopData.per_capita}}</view>
				</view>
			</view>
			<view class="shopCbox">
				<view class="addressCon">
					<view class="address">{{shopData.address}}</view>
					<view class="km">距您{{distance}}公里</view>
				</view>
				<view class="operate">
					<view class="use" @tap="line_to">
						<image src="../../static/images/cc/gps.png" mode="widthFix"></image>
						<view>导航</view>
					</view>
					<view class="use" v-if="shopData.mobile" @tap="callphone(shopData.mobile)">
						<image src="../../static/images/cc/tel.png" mode="widthFix"></image>
						<view>电话</view>
					</view>
				</view>
			</view>
		</view>
		<view class="title">
			<view class="line"></view>
			<view class="titleName">全部套餐</view>
		</view>
		<view class="list" v-for="(item,index) in 0">
			<image
				src="https://qcloud.dpfile.com/pc/UR3o4Rr6AE7MnIxWiwyTExNGm0jeRZnoxf9PPj20DO0opzl7UKxf251v2HlD_Lu0.jpg"
				mode="aspectFill"></image>
			<view class="goods">
				<view class="goodsName">宠粉双人餐(不包含蘸料)</view>
				<view class="goodsLave">可免费配送 | 支持自取</view>
				<view class="evaluate">
					<image src="../../static/images/cc/okk.png" mode="widthFix"></image>
					<view>100%好评</view>
				</view>
				<view class="money">
					<view class="price"><text>￥</text>191</view>
					<view class="original">￥318</view>
				</view>
				<view class="buy">抢购</view>
			</view>
		</view>
		<!-- <image src="../../../static/images/cc/gg.png" mode="widthFix" class="advertisement"></image> -->
		<view class="list" v-for="(item,index) in listData" :key="index">
			<image
				:src="$wanlshop.oss(item.image, 0, 0)"
				mode="aspectFill"></image>
			<view class="goods">
				<view class="goodsName">{{item.title}}</view>
				<!-- <view class="goodsLave">可免费配送 | 支持自取</view> -->
				<view class="goodsLave">{{item.description}}</view>
				
				<view class="evaluate" v-if="item.comment>0">
					<image src="../../static/images/cc/okk.png" mode="widthFix"></image>
					<view>{{Math.floor(100*item.praise/item.comment)}}%好评</view>
				</view>
				<view class="money" v-if="goodsData.user_member == 1 && item.vip_price !== '0.00'">
					<view class="price"><text>￥</text>{{item.vip_price}}</view>
					<view class="original">￥{{item.price}}</view>
				</view>
				<view class="money" v-else>
					<view class="price"><text>￥</text>{{item.price}}</view>
					
				</view>
				<view class="buy" @tap="onGoods(item.id)">抢购</view>
			</view>
		</view>
		<!-- <image src="../../../static/images/cc/gg.png" mode="widthFix" class="advertisement"></image> -->
	</view>
</template>

<script>
	export default {
		data() {
			return {
				shop_id:0,
				headHeight: 0,
				banner:[],
				current_banner_index:0,
				shopData: {
					id: 0,
					avatar: '',
					shopname: '',
					state: 0,
					islive: 0,
					isself: 0,
					categoryStyle: 1,
					find_user:{
						user_no: '',
						fans: 0
					}
				},
				distance:0,
				listData:[],
			};
		},
		created() {
			this.headHeight = (uni.getSystemInfoSync().statusBarHeight + 45) * 2
		},
		onLoad(options){
			let id = 0;
			// 判断是否小程序来源 1.1.9升级
			if(options.hasOwnProperty('scene')){
				let scene = decodeURIComponent(options.scene);
				scene = this.$wanlshop.getParam(scene);
				id = scene.id;
			}else{
				id = options.id;
			}
			
			if('shop_id' in options){
				id = options.shop_id;
			}
			
			// this.loadData(id);
			// if(query.hasOwnProperty('type')){
			// 	this.tabbarId = query.type;
			// }
			
			
			this.shop_id = id;//options.shop_id;
			this.load_banner();
			this.loadData(this.shop_id);
		},
		methods:{
			goBack(){
				uni.navigateBack()
			},
			calculateDistance(lat1, lon1, lat2, lon2) {
			    const R = 6371; // 地球半径（单位：公里）
			    // 将经纬度转换为弧度
			    const lat1Rad = lat1 * Math.PI / 180;
			    const lon1Rad = lon1 * Math.PI / 180;
			    const lat2Rad = lat2 * Math.PI / 180;
			    const lon2Rad = lon2 * Math.PI / 180;
			
			    // 经纬度差值
			    const dLat = lat2Rad - lat1Rad;
			    const dLon = lon2Rad - lon1Rad;
			
			    // Haversine 公式计算
			    const a = Math.sin(dLat / 2) ** 2 + Math.cos(lat1Rad) * Math.cos(lat2Rad) * Math.sin(dLon / 2) ** 2;
			    const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
			    const distance = R * c;
			
			    return distance; // 返回公里数
			},
			callphone(phone){
				uni.makePhoneCall({
					phoneNumber: phone //仅为示例
				});
			},
			line_to(){
				
				uni.openLocation({
					latitude: parseFloat(this.shopData.lat),
					longitude: parseFloat(this.shopData.lng),
					address: this.shopData.address,
					scale: 18,
					success: () => {
					console.log('导航成功');
					}
				});
			},
			async handleFollow() {
				if (this.$store.state.user.isLogin) {
					await uni.request({
						url: '/wanlshop/find/user/setFindUser',
						method: 'POST',
						data: {
							id: this.shopData.find_user.user_no,
							type: 'follow'
						},
						success: res => {
							let concern = this.$store.state.statistics.dynamic.concern;
							if (res.data.data === 0) {
								concern -= 1;
								this.shopData.find_user.fans -= 1;
							} else {
								concern += 1;
								this.shopData.find_user.fans += 1;
							}
							this.$store.commit('statistics/dynamic', {
								concern: concern,
							});
							this.shopData.isFollow = res.data.data;
						}
					});
				} else {
					this.$wanlshop.to('/pages/user/auth/auth')
				}
			},
			getGoods() {
				uni.request({
					url: '/dispatch/shop/goods',
					method: 'GET',
					data: {
						
						shop_id: this.shop_id
					},
					success: res => {
						this.listData = res.data
			
					}
				});
			},
			async loadData(id) {
				let that = this;
				await uni.request({
					url: '/wanlshop/shop/getShopInfo',
					data: {id: id},
					success: res => {
						this.shopData = res.data
						uni.getLocation({
							type: 'gcj02',
							success: function (r) {
								console.log('当前位置的经度：' + r.longitude);
								console.log('当前位置的纬度：' + r.latitude);	
								const beijing = { lat: parseFloat(that.shopData.lat), lng: parseFloat(that.shopData.lng) };
								const shanghai = { lat: r.latitude, lng: r.longitude };
								const km = that.calculateDistance(beijing.lat, beijing.lng, shanghai.lat, shanghai.lng);
								// console.log(km.toFixed(2) + ' 公里');
								that.distance = km.toFixed(2)
							}
						});	
						that.getGoods();
						
						//  获取当前位置
						// 示例用法
						// const beijing = { lat: 39.9042, lng: 116.4074 };
						// const shanghai = { lat: 31.2304, lng: 121.4737 };
						// const km = calculateDistance(beijing.lat, beijing.lng, shanghai.lat, shanghai.lng);
						// console.log(km.toFixed(2) + ' 公里');
					}
				});
			},
			change_banner(event){
				this.current_banner_index = event.detail.current;
			},
			load_banner(){
				uni.request({
					url: '/dispatch/shop/getConfig',
					method:"POST",
					data: {
						shop_id:this.shop_id
					},
					success: res => {
						console.log(res);
						if(res.data.banner){
							this.banner = res.data.banner;
						}else{
							this.banner = res.data.default_banner;
						}
					}
				});
			},
		}
	}
</script>

<style lang="scss">
	page {
		background-color: #F7F7F7;
	}

	.headcontent {
		width: 750rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		position: fixed;
		top: 0;
		z-index: 6666;

		.contentbox {
			width: 750rpx;
			display: flex;
			position: relative;
			align-items: center;

			image {
				width: 50rpx;
				height: 50rpx;
				margin-left: 20rpx;
				position: relative;
				z-index: 999;
			}
		}
	}

	.banner {
		width: 750rpx;
		height: 498rpx;
		position: relative;

		.shadow {
			width: 750rpx;
			height: 498rpx;
			position: absolute;
			top: 0;
			z-index: 1;
			background: linear-gradient(180deg, rgba(0, 0, 0, 0.7) 0%, rgba(0, 0, 0, 0) 40%);
		}

		swiper {
			width: 750rpx;
			height: 498rpx;
			position: relative;



			.swiper-item {
				width: 750rpx;
				height: 498rpx;
				display: flex;
				justify-content: center;
				align-items: center;

				image {
					width: 100%;
					height: 100%;
				}
			}
		}

		.dropBox {
			width: 700rpx;
			margin-left: 34rpx;
			display: flex;
			position: absolute;
			bottom: 60rpx;

			.drop {
				width: 46rpx;
				height: 7rpx;
				background: rgba(255, 255, 255, 0.3);
				border-radius: 50rpx;
				margin-right: 8rpx;
			}

			.select {
				background-color: #fff;
			}
		}
	}

	.shop {
		width: 750rpx;
		margin-top: -45rpx;
		position: relative;
		background-color: #F7F7F7;
		border-radius: 40rpx;
		display: flex;
		flex-wrap: wrap;
		justify-content: center;
		padding-top: 45rpx;

		.shopAbox {
			width: 664rpx;
			display: flex;
			justify-content: space-between;
			align-items: center;

			.shopCon {
				display: flex;
				align-items: center;

				image {
					width: 100rpx;
					height: 100rpx;
					border-radius: 50%;
				}

				.shopDetail {
					width: 400rpx;
					margin-left: 32rpx;

					.shopName {
						font-size: 33rpx;
						color: #3D3D3D;
					}

					.shopNum {
						font-size: 26rpx;
						color: #999999;
						margin-top: 10rpx;

						text {
							color: #FB4E2F;
							margin-right: 10rpx;
						}
					}
				}
			}

			.follow {
				width: 157rpx;
				height: 65rpx;
				border: 1px solid #FB4E2F;
				color: #FB4E2F;
				font-size: 30rpx;
				border-radius: 10rpx;
				display: flex;
				justify-content: center;
				align-items: center;
			}
		}

		.shopBbox {
			width: 664rpx;
			display: flex;
			justify-content: space-between;
			align-items: center;
			margin-top: 33rpx;

			.shopLcon {
				width: 200rpx;
				height: 32rpx;
				background: rgba(251, 78, 47, 0.08);
				border-radius: 50rpx;
				display: flex;

				.divide {
					width: 72rpx;
					height: 32rpx;
					background-color: #FB4E2F;
					border-radius: 50rpx 0px 8rpx 50rpx;
					display: flex;
					justify-content: center;
					align-items: center;
					font-size: 26rpx;
					color: #fff;
				}

				.star {
					width: 138rpx;
					height: 32rpx;
					display: flex;
					justify-content: center;
					align-items: center;

					image {
						width: 16rpx;
						height: 16rpx;
						margin-left: 4rpx;
						margin-right: 4rpx;
					}
				}
			}

			.label {
				display: flex;
				align-items: center;

				view {
					font-size: 26rpx;
					color: #999999;
					margin-left: 38rpx;
				}
			}
		}

		.shopCbox {
			width: 664rpx;
			display: flex;
			justify-content: space-between;
			align-items: center;
			margin-top: 40rpx;

			.addressCon {
				width: 470rpx;

				.address {
					font-size: 30rpx;
					color: #3D3D3D;
				}

				.km {
					font-size: 26rpx;
					color: #999999;
					margin-top: 10rpx;
				}
			}

			.operate {
				display: flex;

				.use {
					width: 52rpx;
					margin-left: 42rpx;
					display: flex;
					flex-wrap: wrap;
					justify-content: center;

					image {
						width: 52rpx;
						height: 52rpx;
					}

					view {
						font-size: 26rpx;
						color: #999999;
					}
				}
			}
		}
	}

	.title {
		width: 664rpx;
		margin-left: 43rpx;
		display: flex;
		align-items: center;
		margin-top: 50rpx;
		margin-bottom: 30rpx;

		.line {
			width: 8rpx;
			height: 34rpx;
			background-color: #FB4E2F;
			border-radius: 50rpx;
		}

		.titleName {
			font-size: 33rpx;
			color: #3D3D3D;
			margin-left: 17rpx;
		}
	}

	.list {
		width: 690rpx;
		background-color: #fff;
		padding: 28rpx 0;
		margin-left: 30rpx;
		border-radius: 20rpx;
		display: flex;
		margin-bottom: 20rpx;

		image {
			width: 200rpx;
			height: 200rpx;
			border-radius: 10rpx;
			margin-left: 26rpx;
		}

		.goods {
			width: 416rpx;
			margin-left: 22rpx;
			display: flex;
			align-content: space-between;
			flex-wrap: wrap;
			position: relative;

			.goodsName {
				width: 100%;
				font-size: 30rpx;
				color: #3D3D3D;
			}

			.goodsLave {
				width: 100%;
				font-size: 26rpx;
				color: #999999;
			}

			.evaluate {
				width: 157rpx;
				height: 39rpx;
				border-radius: 50rpx;
				display: flex;
				justify-content: center;
				align-items: center;
				background-color: #F7F7F7;

				image {
					width: 20rpx;
					height: 20rpx;
					margin-left: 0 !important;
				}

				view {
					font-size: 22rpx;
					color: #666666;
					margin-left: 6rpx;
				}
			}

			.money {
				width: 100%;
				display: flex;
				align-items: flex-end;

				.price {
					color: #FB4E2F;
					font-size: 36rpx;

					text {
						font-size: 26rpx;
					}
				}

				.original {
					font-size: 26rpx;
					color: #999999;
					margin-left: 10rpx;
					text-decoration: line-through;
					margin-bottom: 4rpx;
				}
			}

			.buy {
				position: absolute;
				bottom: 0;
				right: 0;
				width: 136rpx;
				height: 65rpx;
				background-color: #FB4E2F;
				display: flex;
				justify-content: center;
				align-items: center;
				border-radius: 50rpx;
				font-size: 26rpx;
				color: #fff;
			}
		}
	}

	.advertisement {
		width: 690rpx;
		margin-left: 30rpx;
		margin-bottom: 20rpx;
	}
</style>